<script setup lang="ts">
import dynamic from '@/components/svg/dynamic.vue'
import hor from '@/components/svg/hor.vue'
import { ref } from 'vue'
import homeApi from '@/service/api/home'
import type { TabListItem } from '@/types/page/tab'
const emit = defineEmits(['change'])
const institutionList = ref<TabListItem[]>([])
const getInstitution = async () => {
  const { data } = await homeApi.institution()
  institutionList.value = [
    {
      id: 0,
      name: '全部'
    },
    ...data
  ]
}
const handTab = (e: TabListItem) => {
  emit('change', toRaw(e))
}
getInstitution()

const appendInstitution = computed(() => {
  return institutionList.value.slice(0, 17)
})

const lastInstitution = computed(() => {
  return institutionList.value.slice(17)
})

const { width } = useWindowSize()
</script>

<template>
  <div class="classify" ref="target">
    <template v-if="width <= 800">
      <div class="row">
        <div class="tab">
          <div class="icon">
            <dynamic />
          </div>
          <span>动态</span>
        </div>
        <div class="tab">
          <div class="icon">
            <hor />
          </div>
          <span>热门</span>
        </div>
        <div class="classify-right">
          <div class="item">专栏</div>
          <div class="item">活动</div>
          <div class="item">社区中心</div>
          <div class="item">直播</div>
          <div class="item">课堂</div>
          <div class="item">热歌新榜</div>
        </div>
      </div>
      <div class="leixing">
        <div
          class="btn"
          v-for="item in institutionList"
          :key="item.id"
          @click="handTab(item)"
        >
          {{ item.name }}
        </div>
      </div>
    </template>
    <template v-else>
      <div class="tab">
        <div class="icon">
          <dynamic />
        </div>
        <span>动态</span>
      </div>
      <div class="tab">
        <div class="icon">
          <hor />
        </div>
        <span>热门</span>
      </div>
      <div class="leixing">
        <div
          class="btn"
          v-for="item in appendInstitution"
          :key="item.id"
          @click="handTab(item)"
        >
          {{ item.name }}
        </div>
        <n-popover
          v-if="lastInstitution.length > 0"
          style="
            --n-border-radius: 4px;
            --n-box-shadow: 0 28px 50px rgba(25, 24, 40, 0.35);
          "
          :show-arrow="false"
          class="leixing-more-pop"
          placement="bottom-end"
          trigger="hover"
        >
          <template #trigger>
            <div class="btn">更多</div>
          </template>
          <div class="norem-pop-dia">
            <n-space>
              <n-button
                v-for="item in lastInstitution"
                :key="item.id"
                quaternary
                @click="handTab(item)"
              >
                {{ item.name }}
              </n-button>
            </n-space>
          </div>
        </n-popover>
      </div>
      <div class="classify-right">
        <div class="item">专栏</div>
        <div class="item">活动</div>
        <div class="item">社区中心</div>
        <div class="item">直播</div>
        <div class="item">课堂</div>
        <div class="item">热歌新榜</div>
      </div>
    </template>
  </div>
</template>

<style scoped lang="scss">
.classify {
  //width: 1800px;
  margin: 0 auto;
  display: flex;
  padding: 20px 24px 0 24px;
  .tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    .icon {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      background: #ff9212;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    span {
      margin-top: 5px;
      font-size: 12px;
    }
    &:nth-child(2) {
      margin-left: 24px;
      .icon {
        background: #f07775;
      }
    }
  }
  .leixing {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    margin-left: 24px;
    gap: 10px 15px;
    .btn {
      letter-spacing: 2px;
      font-size: 11px;
      width: 100px;
      height: 30px;
      line-height: 30px;
      border: 1px solid #f1f2f3;
      border-radius: 6px;
      background-color: #f6f7f8;
      text-align: center;
      font-weight: 400;
      transition: background-color 0.3s, color 0.3s;
      cursor: pointer;
      color: #61666d;
      &:hover {
        background-color: #e3e5e7;
        color: #18191c;
      }
    }
  }
  &-right {
    margin-left: 24px;
    width: 250px;
    display: flex;
    flex-wrap: wrap;
    border-left: 1px solid #f2f2f2;
    padding-top: 10px;
    padding-left: 40px;
    gap: 10px 20px;
    .item {
      padding-left: 20px;
      font-size: 12px;
      cursor: pointer;
      transition: color 0.15s;
      &:hover {
        color: var(--n-color);
      }
    }
  }
}
@media (max-width: 800px) {
  .classify {
    display: block;
    padding: 15px 12px 0 12px;
    .row {
      display: flex;
      align-items: center;
      .tab {
        .icon {
          width: 25px;
          height: 25px;
        }
        span {
          margin-top: 2px;
          font-size: 11px;
        }
      }
    }
    &-right {
      margin-left: 15px;
      flex: 1;
      width: initial;
      padding-left: 10px;
      gap: 5px 10px;
      padding-top: 0;
    }

    .leixing {
      margin: 10px 0 0 0;
      gap: 10px;
      .btn {
        width: 89px;
      }
    }
  }
}
//@media (max-width: 1300px) {
//  .classify {
//    .leixing {
//      .btn {
//        font-size: 16px;
//      }
//    }
//  }
//}
</style>
<style lang="scss">
.leixing-more-pop {
  width: 340px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
</style>
